package com.xiaobai.frame.controller;


import com.xiaobai.common.utils.config.page.Pager;
import com.xiaobai.common.utils.result.R;
import com.xiaobai.common.utils.result.Result;
import com.xiaobai.common.utils.result.ResultCode;
import com.xiaobai.common.utils.tool.Func;
import com.xiaobai.frame.service.IUserService;
import com.xiaobai.model.dto.system.userDTO.UserInfo;
import com.xiaobai.model.entity.system.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-07
 */
@RestController
@RequestMapping("/user")
@AllArgsConstructor
@Tag(name = "用户表数据")
public class UserController {

    private final IUserService iUserService;

    @PostMapping
    @Operation(summary = "新增用户", description = "管理员新增")
    public Result addUserInfo(@RequestBody UserInfo userInfo) throws Exception {
        return iUserService.addUserInfo(userInfo);
    }

    @PostMapping("login")
    @Operation(summary = "账号登录", description = "登录接口")
    public Result login(@RequestParam("account") String account,
                        @RequestParam("password") String password) {
        return iUserService.login(account, password);
    }

    @PostMapping("refresh-token")
    @Operation(summary = "刷新token", description = "传刷新token重新获取token")
    public Result refreshToken(@RequestParam("refreshToken") String refreshToken) {
        return iUserService.refreshToken(refreshToken);
    }

    @PostMapping("logout")
    @Operation(summary = "退出登录")
    public Result logout() {
        return iUserService.logout();
    }

    @PutMapping("reset-password")
    @Operation(summary = "重置密码" , description = "如果需要重置多个id之间用逗号连接")
    public Result updateResetPassword(@RequestParam("ids") String ids) throws Exception {
        return iUserService.resetPassword(ids);
    }

    @PutMapping
    @Operation(summary = "修改用户信息")
    public Result updateUserInfo(@RequestBody User user) throws Exception {
        return iUserService.updateUserInfo(user);
    }

    @GetMapping
    @Operation(summary = "获取账号信息")
    public Result getUserInfo() {
        return iUserService.getUserInfo();
    }

    @DeleteMapping
    @Operation(summary = "删除用户", description = "管理员使用,多个用户用逗号连接")
    public Result deleteUser(Long[] ids) {
        if (Func.isEmpty(ids)) {
            return R.fail(ResultCode.PARAMETER_NULL, "传入数据为空");
        }
        if (iUserService.deleteLogic(Arrays.asList(ids))) {
            return R.success(ResultCode.SUCCESS, "删除成功");
        }else {
            return R.fail(ResultCode.INTERNAL_SERVER_ERROR,"删除失败,请联系管理人员");
        }
    }

    @Pager
    @GetMapping("list")
    @Operation(summary = "查找所有账户信息")
    public Result listUserInfo() {
        return R.success(iUserService.listUserInfo());
    }

}
